package cn.changeforyou.analyzer.sqlparse;

import cn.changeforyou.analyzer.ParseException;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement;

/**
 * sql解析工具
 * @author zhyu
 * @date 2021/2/10 9:18
 */
public class SqlParserUtil {
    
    public static String getMysqlTableCommentByCreateTableSql(String sql) {
        SQLStatement statement = SQLUtils.parseSingleMysqlStatement(sql);
        if (statement instanceof MySqlCreateTableStatement) {
            MySqlCreateTableStatement statement1 = (MySqlCreateTableStatement) statement;
            SQLExpr comment = statement1.getComment();
            if(null == comment) {
                return null;
            }
            return comment.toString();
        }
        throw new ParseException();
    }
}
